// #ifdef APP-PLUS
const printModule = uni.requireNativePlugin('PrintModule');

// #endif
export const connectBlue = (device) => {
  return new Promise((resolve, reject) => {
    // 调用异步方法
    uni.closeBLEConnection({
      deviceId: device.deviceId
    });
    uni.createBLEConnection({
      deviceId: device.deviceId,
      timeout: 6000,
      success(res) {
        printModule.connectBT(
          {
            address: device.deviceId,
            timeout: 3000
          },
          (ret) => {
            if (ret.result === 0) {
              resolve(true)
            } else {
              uni.showToast({
                title: '连接失败，请重试',
                icon: 'none',
                duration: 1500
              })
              reject(false)
            }
          }
        )
      }
    })

  })
}

export const connectChange = (device) => {
  return new Promise((resolve, reject) => {
    uni.onBLEConnectionStateChange((res) => {
      if (res.connected) {
        resolve(true)
      } else {
        reject(false)
        uni.closeBLEConnection({
          deviceId: device.deviceId
        });
        printModule.closePort();
      }
    })
  })
}



// 实验
export const printMiddleCheck = (data) => {
  printModule.labelStart();
  printModule.printQRCode(
    {
      x: '15',
      y: '195',
      orientation: 'N',
      magnification: '2',
      size: '7',
      data: data.checkCode,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '170',
      y: '25',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `报检单号：${data.checkCode}`,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '170',
      y: '63',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `产品名称：${data.invName}`,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '170',
      y: '101',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `样品批号：${data.sampleBatch}`,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '170',
      y: '139',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `工　　序：${data.processName}`,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '170',
      y: '177',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `规　　格：${data.invStd}`,
    },
    (ret) => { },
  );
  printModule.labelEnd();
};

// 外来样
export const printExoticCheck = (data) => {
  printModule.labelStart();
  printModule.printQRCode(
    {
      x: '20',
      y: '195',
      orientation: 'N',
      magnification: '2',
      size: '6',
      data: data.checkCode,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '200',
      y: '40',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `报检单号：${data.checkCode}`,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '200',
      y: '80',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `样品编码：${data.sampleCode}`,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '200',
      y: '120',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `样品批号：${data.sampleBatch}`,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '200',
      y: '160',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `规　　格：${data.invStd}`,
    },
    (ret) => { },
  );
  printModule.labelEnd();
};

// 报告
export const printReportCode = (data) => {
  printModule.labelStart();
  printModule.printQRCode(
    {
      x: '20',
      y: '195',
      orientation: 'N',
      magnification: '2',
      size: '6',
      data: `${data.reportCode}-${data.analysisCode}-${data.itemCode}-`,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '200',
      y: '40',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `报告单号：${data.reportCode}`,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '200',
      y: '100',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `分析手段：${data.analysisName}`,
    },
    (ret) => { },
  );
  printModule.printText(
    {
      x: '200',
      y: '160',
      type: 7,
      orientation: 'N',
      size: 3,
      data: `检测指标：${data.itemName}`,
    },
    (ret) => { },
  );
  printModule.labelEnd();
};